package com.sn.pw.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sn.pw.dao.entity.Company;
import com.sn.pw.dao.entity.Evaluation;
import com.sn.pw.dao.entity.User;
import com.sn.pw.dao.mapper.CompanyMapper;
import com.sn.pw.dao.mapper.EvaluationMapper;
import com.sn.pw.dao.mapper.UserMapper;
import com.sn.pw.page.ConventPage;
import com.sn.pw.page.PageReq;
import com.sn.pw.page.PageResp;
import com.sn.pw.service.EvaluationService;
import com.sn.pw.vo.createreq.EvaluationCreateReq;
import com.sn.pw.vo.req.EvaluationListReq;
import com.sn.pw.vo.req.IdReq;
import com.sn.pw.vo.resp.CompanyResp;
import com.sn.pw.vo.resp.EvaluationResp;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("evaluationService")
@RequiredArgsConstructor(onConstructor_ = @Autowired)
public class EvaluationServiceImpl extends ServiceImpl<EvaluationMapper, Evaluation> implements EvaluationService {

    private final CompanyMapper companyMapper;

    @Override
    public List<EvaluationResp> getEvaluationListByUserId(IdReq idReq) {
        LambdaQueryWrapper<Evaluation> lambdaQueryWrapper = Wrappers.<Evaluation>lambdaQuery().eq(Evaluation::getEvaluatorId, idReq.getId());
        List<Evaluation> list = super.list(lambdaQueryWrapper);
        return BeanUtil.copyToList(list, EvaluationResp.class);
    }

    @Override
    public List<EvaluationResp> getEvaluationListByCompanyId(IdReq idReq) {
        LambdaQueryWrapper<Evaluation> lambdaQueryWrapper = Wrappers.<Evaluation>lambdaQuery().eq(Evaluation::getCompanyId, idReq.getId());
        List<Evaluation> list = super.list(lambdaQueryWrapper);
        return BeanUtil.copyToList(list, EvaluationResp.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean create(EvaluationCreateReq param) {
        return super.save(BeanUtil.toBean(param, Evaluation.class));
    }

    @Override
    public List<EvaluationResp> getEvaluationList(EvaluationListReq evaluationListReq) {
        LambdaQueryWrapper<Evaluation> lambdaQueryWrapper = Wrappers.<Evaluation>lambdaQuery().like(StringUtils.hasText(evaluationListReq.getShippingOrderNumber()), Evaluation::getShippingOrderNumber, evaluationListReq.getShippingOrderNumber());
//        IPage<Evaluation> page = super.page(ConventPage.getPage(evaluationListReq), lambdaQueryWrapper);
//        PageResp<EvaluationResp> pageResp = ConventPage.getPageResp(page, CompanyResp.class);
        List<Evaluation> list = super.list(lambdaQueryWrapper);

        Map<Long, Company> companyMap = new HashMap<>();
        List<Company> companies = companyMapper.selectList(null);
        if (StringUtils.hasText(evaluationListReq.getCompanyName()))
            companies = companies.stream()
                    .filter(company -> company.getCompanyName().contains(evaluationListReq.getCompanyName()))
                    .collect(Collectors.toList());

        for (Company company : companies)
            companyMap.put(company.getCompanyId(), company);

        return list.stream()
                .filter(evaluation -> companyMap.get(evaluation.getCompanyId()) != null)
                .map(evaluation -> {
                    EvaluationResp evaluationResp = BeanUtil.toBean(evaluation, EvaluationResp.class);
                    evaluationResp.setCompanyName(companyMap.get(evaluation.getCompanyId()).getCompanyName());
                    return evaluationResp;
                })
                .collect(Collectors.toList());
    }
}
